package com.ymatou.iapi.livequery.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livequery.service.GetActivityInfoCall;
import com.ymatou.iapi.sellerproduct.service.AddLiveProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.LiveBasicInfoBean;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
/**
*直播详情接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetActivityInfo{
	private static GetActivityInfoCall getactivityinfoCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播详情接口");
	}

	@Before
	public void caseUp() {
		getactivityinfoCall=new GetActivityInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getactivityinfoCall.getOpurl());
	}
	
	
	/**
	 * 报文断言
	 * @param dataStr
	 * @throws Exception
	 */
	public void getReturnCode(JSONObject dataStr) throws Exception {
	    try {	    	
			//返回报文断言		
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(0, bCode, "\"BCode\":200");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("操作成功", msg, "\"Msg\":操作成功");	
	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}
	
		/**
		 * sql断言映射关系
		 * @return
		 * @throws Exception
		 */
		public Map<String,String> sqlMap() throws Exception {
		    try {	    	
		    	Map<String,String> sqlMap = new HashMap<String,String>();			
				sqlMap.put("Data->Title", "sTitle");	
				sqlMap.put("Data->ActivityContent", "sContent");	
				sqlMap.put("Data->ActivityName", "sName");	//直播名称
				sqlMap.put("Data->SellerId", "iSellerId");	
				sqlMap.put("Data->Seller", "sSellerName");	
				sqlMap.put("Data->ShopAddress", "sPosition");	//卖家地址
				sqlMap.put("Data->ActivityPicture", "sPicUrl");	
				sqlMap.put("Data->Action", "iAction");	
				sqlMap.put("Data->Sort", "iSort");	
				return sqlMap;
		    }catch (Exception e) {  
		        e.printStackTrace();	        
		    }  
		    return null;	
	}
	
		/**
		 * 映射老Mango库关系
		 * @return
		 * @throws Exception
		 */
		public Map<String,String> oldMangoMap() throws Exception {
		    try {	    	
		    	Map<String,String> mangoMap = new HashMap<String,String>();			
		    	mangoMap.put("Data->Title", "Title");	
		    	mangoMap.put("Data->ActivityContent", "ActivityContent");	
		    	mangoMap.put("Data->ActivityName", "ActivityName");	//直播名称
		    	mangoMap.put("Data->ActivityInfo", "ActivityInfo");	//直播名称
		    	mangoMap.put("Data->SellerId", "SellerId");	
		    	mangoMap.put("Data->Seller", "Seller");	
		    	mangoMap.put("Data->SellerLogo", "SellerLogo");	 //卖家头像
		    	mangoMap.put("Data->ShopAddress", "ShopAddress");	
				mangoMap.put("Data->ActivityPicture", "ActivityPicture");	
				mangoMap.put("Data->Action", "Action");	
				mangoMap.put("Data->Country", "Country");
				return mangoMap;
		    }catch (Exception e) {  
		        e.printStackTrace();	        
		    }  
		    return null;	
	}

		
		

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityInfo_001() {
		Logger.start(true,"查询直播详情（未开始的直播无商品）");
		try {
			//TODO 补全case代码
			Integer activityId = LiveCallService.createLiveBeginInFuture(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			
			Map updateMap = new HashMap<String, Object>();
			updateMap.put("ActivityId", activityId);
			
			JSONObject result =  getactivityinfoCall.callAndGetReturnData(updateMap);
			
			//报文断言
			getReturnCode(result);
			//无直播商品部分的断言
			checkGetActivityInfo(result, activityId);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityInfo_002() {
		Logger.start(true,"查询直播详情（未开始的直播有商品）");
		try {
			//TODO 补全case代码
			Integer activityId = LiveCallService.createLiveBeginInFuture(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");			
//			JSONObject productResponseBean = new AddLiveProductCall().addLiveProductWithLiveId(sellerId, activityId);
			
			String productID = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			SellerProductCallService.addProductInLive(sellerId, activityId, productID);
			

			//将商品添加到直播
			SellerProductCallService.addProductInLive(sellerId, activityId, productID);
			
			Map updateMap = new HashMap<String, Object>();
			updateMap.put("ActivityId", activityId);
			
			JSONObject result =  getactivityinfoCall.callAndGetReturnData(updateMap);
			
			//报文断言
			getReturnCode(result);
			//无直播商品部分的断言
			checkGetActivityInfo(result, activityId);
			
			//报文BrandList部分Mango映射关系
	    	Map<String,String> BrandListmangoMap = new HashMap<String,String>();			
	    	BrandListmangoMap.put("Data->BrandList[0]->BrandEnName", "BrandEN");
	    	BrandListmangoMap.put("Data->BrandList[0]->BrandId", "BrandId");	
	    	BrandListmangoMap.put("Data->BrandList[1]->BrandName", "ThirdCategoryName");	
	    	BrandListmangoMap.put("Data->BrandList[1]->CategoryId", "ThirdCategoryId");	
	    	BrandListmangoMap.put("Data->BrandList[1]->ParentName", "CategoryName");	
	    	BrandListmangoMap.put("Data->BrandList[1]->ParentId", "CategoryId");
	    	
	    	//Mango
			Map<String, Object> liveProductInfo = new LiveProductInfoWapper().getliveproductByLiveId(activityId);
			AssertService.assertResultEqual(BrandListmangoMap,result, liveProductInfo);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	
	
	
	
	
	/**
	 * 
	 * @param result 返回报文
	 * @param activityId 直播id
	 * @throws Exception
	 */
	public void checkGetActivityInfo(JSONObject result,int activityId) throws Exception {
	    try {	    		
			//取出时间
			String sStartTime = result.getJSONObject("Data").getString("StartTime");
			String sEndTime = result.getJSONObject("Data").getString("EndTime");
			String sAddTime = result.getJSONObject("Data").getString("AddTime");
	    	
			//Mango时间差8小时
			String sStartTimeforMongo = YMTDateUtil.getBeforeOrNextHour(sStartTime,-8);
			String sEndTimefroMongo = YMTDateUtil.getBeforeOrNextHour(sEndTime,-8);
			String sAddTimeforMongo = YMTDateUtil.getBeforeOrNextHour(sAddTime,-8);						
			
			//报文断言 
			getReturnCode(result);
			
			//AppProductRelease库  Ymt_Activity
			List<Map> productsInActivity =  (List<Map>) new YmtLiveWapper().selectforMapByLiveId(activityId);
			HashMap<String, Object> actRetMapbysql = (HashMap<String, Object>) productsInActivity.get(0); 
			
			//获取sql映射关系
			Map<String,String> sqlMap = sqlMap();
			//SQL断言
			AssertService.assertResultEqual(sqlMap,result, actRetMapbysql);
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(actRetMapbysql.get("dBeginTime")), sStartTime, "SQL开始时间对比"); 
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(actRetMapbysql.get("dEndTime")), sEndTime, "SQL结束时间对比"); 
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(actRetMapbysql.get("dAddTime")), sAddTime, "SQL创建时间对比");
			
			//Mongo老库映射关系
			Map<String,String> oldMongoMap = oldMangoMap();
			//old Mango
			LiveBasicInfoBean liveBasicInfoBean =new LiveBasicInfoWapper().getByActivityId(activityId);
			Map<String,Object> actRetMapbyoldMango = MapUtil.pojoToMap(liveBasicInfoBean);
			AssertService.assertResultEqual(oldMongoMap,result, actRetMapbyoldMango);
			Logger.verifyEquals(YMTDateUtil.getFromISODate(actRetMapbyoldMango.get("StartTime").toString()),sStartTimeforMongo, "Mango开始时间对比");
			Logger.verifyEquals(YMTDateUtil.getFromISODate(actRetMapbyoldMango.get("EndTime").toString()),sEndTimefroMongo, "Mango结束时间对比");
			Logger.verifyEquals(YMTDateUtil.getFromISODate(actRetMapbyoldMango.get("AddTime").toString()),sAddTimeforMongo, "Mango创建时间对比");
	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}
	
	
} 